﻿using Mapster;
using SqlSugar;
using System.ComponentModel;
using System.Reflection;
using Zhp.Entity.Formula;
using Zhp.IService.Formula;
using Zhp.Model.Formula;
using Zhp.Service.Base;
using Zhp.SqlSugar.Repository;

namespace Zhp.Service.Formula
{
    public class FormulaVariableService : BaseService<FormulaVariable>, IFormulaVariableService
    {
        public FormulaVariableService(IBaseRepository<FormulaVariable> rep)
        {
            _rep = rep;
        }

        public async Task<List<FormulaVariableModel>> Query(FormulaVariableSearch search)
        {
            var list = await _rep.GetPageListAsync(x => true, search, x => x.Order, OrderByType.Asc);

            return list.Adapt<List<FormulaVariableModel>>();
        }

        public async Task<bool> Create(FormulaVariableEdit p)
        {
            return await p.Adapt<FormulaVariable>().InsertAsync();
        }

        public async Task<bool> Update(FormulaVariableEdit p)
        {
            var entity = await _rep.GetByIdAsync(p.Id);
            if (entity != null)
            {
                p.Adapt(entity);
                return await _rep.UpdateAsync(entity);
            }
            return false;
        }

        public async Task<bool> Delete(long id)
        {
            return await _rep.DeleteByIdAsync(id);
        }

        public async Task<List<FormulaVariableList>> QueryFormulaVariable()
        {
            var list = await _rep.GetListAsync();

            list = list.OrderBy(x => x.Order).ToList();

            return list.Adapt<List<FormulaVariableList>>();
        }

        public async Task<List<FormulaVariableSelect>> QueryVariableClass()
        {
            var list = new List<FormulaVariableSelect>();
            var types = GetVariableClassTypes();
            foreach (var item in types)
            {
                var attr = item.GetCustomAttribute<DisplayNameAttribute>();
                list.Add(new FormulaVariableSelect
                {
                    Text = attr != null ? attr.DisplayName : item.Name,
                    Value = item.FullName
                });
            }
            return await Task.FromResult(list);
        }

        public async Task<List<FormulaVariableSelect>> QueryVariable(string variableClass)
        {
            var list = new List<FormulaVariableSelect>();
            if (string.IsNullOrEmpty(variableClass))
            {
                return list;
            }
            var type = GetVariableClassType(variableClass);
            var props = type.GetProperties();
            foreach (var prop in props)
            {
                var attr = prop.GetCustomAttribute<DisplayNameAttribute>();
                if (attr != null)
                {
                    list.Add(new FormulaVariableSelect
                    {
                        Text = attr?.DisplayName,
                        Value = prop.Name
                    });
                }
            }
            return await Task.FromResult(list);
        }

        public Type GetVariableClassType(string variableClass)
        {
            return GetVariableClassTypes().First(x => x.FullName == variableClass);
        }

        /// <summary>
        /// 获取变量类所在程序集，获取的是Zhp.IService
        /// </summary>
        /// <returns></returns>
        private Type[] GetVariableClassTypes()
        {
            var type = typeof(IFormulaVariable);

            return Assembly.GetAssembly(type).GetTypes().Where(x => type.IsAssignableFrom(x) && !x.IsInterface).ToArray();
        }
    }
}
